<body>
<canvas id="c" width="600" height="100"></canvas>
<script>
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");

var gradient = ctx.createLinearGradient(0, 0, 0, 100);
gradient.addColorStop(0.4, "#f00");
gradient.addColorStop(0.6, "#00f");

ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 100, 100);

gradient.addColorStop(0, "#fff");
gradient.addColorStop(1, "#fff");
ctx.fillStyle = gradient;
ctx.fillRect(150, 0, 100, 100);
</script>
</body>
